#include "text.hpp"

using cir_queue = circular_queue<int>;
using _mypthread = mypthread<cir_queue>;

void producer_run(cir_queue &que)
{
    int i = 0;
    while (1)
    {
        sleep(1);
        que.push(i);
        cout << "producer_Run: "<<  i << endl;
        i++;
    }
}

void consumer_run(cir_queue &que)
{
    int i = 0;
    while (1)
    {
        sleep(1);
        que.pop(&i);
        cout << "consumer_Run: "<<  i << endl;
        
    }
}


void create_producer(vector<_mypthread> *v, int size, cir_queue &que)
{
    for (int i = 0; i < size; i++)
    {
        string name = "producer_" + to_string(i);
        v->push_back(_mypthread(name, producer_run, que));
    }
}

void create_consumer(vector<_mypthread> *v, int size, cir_queue &que)
{
    for (int i = 0; i < size; i++)
    {
        string name = "consumer_" + to_string(i);
        v->push_back(_mypthread(name, consumer_run, que));
    }
}

void mypthread_run(vector<_mypthread> *v)
{
    for (auto &e : *v)
    {
        e.start();
    }
}

void mypthread_join(vector<_mypthread> *v)
{
    for (auto &e : *v)
    {
        e.join();
    }
}

int main()
{
    vector<_mypthread> v;

    cir_queue *que = new cir_queue(10);

    // 创建生产者
    create_producer(&v, 1, *que);

    // 创建消费者
    create_consumer(&v, 1, *que);

    // 统一启动
    mypthread_run(&v);

    // 统一回收
    mypthread_join(&v);

    return 0;
}